package com.baidu.unlockdemo.customview

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class MusicWaveView: View {
    constructor(context: Context) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){}

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var widthSize = MeasureSpec.getSize(widthMeasureSpec)

        if (widthMode != MeasureSpec.EXACTLY) {
            widthSize = dp2pxF(200).toInt()
        }

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        var heightSize = MeasureSpec.getSize(heightMeasureSpec)

        if (heightMode != MeasureSpec.EXACTLY) {
            heightSize = dp2pxF(200).toInt()
        }

        setMeasuredDimension(widthSize, heightSize)
    }

    //记录间距
    private var space = dp2pxF(40)
    private var mWidth = 0f
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#00BAAD")
        style = Paint.Style.FILL
    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = (measuredWidth - 2 * space) / 3
    }

    //动画因子：高度的比例
    private val mTopOffsetRateArray = arrayOf(1f,1f,1f)
    private val mAnimators = arrayListOf<ValueAnimator>()
    private val mColors = arrayListOf<Int>().apply {
        add(Color.parseColor("#00BAAD"))
        add(Color.parseColor("#FF8D1A"))
        add(Color.parseColor("#AC33C1"))
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        for (i in 0..2) {
            mPaint.color = mColors[i]
            //绘制矩形
            canvas.drawRect(
                i*(space+mWidth),
                mTopOffsetRateArray[i]*measuredHeight,
                mWidth+i*(space+mWidth),
                measuredHeight.toFloat(),
                mPaint
            )
        }
    }

    private fun createAnimators(){
        for (i in 0..2) {
            ValueAnimator.ofFloat(1f, 0f).apply {
                duration = 500
                repeatCount = ValueAnimator.INFINITE
                repeatMode = ValueAnimator.REVERSE
                startDelay = i*200L
                addUpdateListener {
                    mTopOffsetRateArray[i] = it.animatedValue as Float
                    invalidate()
                }
                mAnimators.add(this)
            }
        }
    }

    fun start(){
        //没有就创建
        if (mAnimators.isEmpty()){
            createAnimators()
        }
        //有了就判断是否正在动画
        //if (mAnimators[0].isRunning || mAnimators[0].isStarted) return

        //启动动画
        mAnimators.forEach {
            //是否是暂停状态
            if (it.isPaused){
                //恢复播放
                it.resume()
            }else{
                //重新启动
                it.start()
            }
        }
    }

    fun stop(){
        if (mAnimators.isEmpty()) return

        mAnimators.forEach {
            if (it.isStarted || it.isRunning) it.pause()
        }
    }
}